//
//  MTNewCardCreateVC.m
//  met
//
//  Created by 李晓俊 on 2019/6/6.
//  Copyright © 2019 李晓俊. All rights reserved.
//

#import "MTNewCardCreateVC.h"
#import "SensitiveWordTools.h"
#import "CustomTextView.h"
NSInteger const MAX_STARWORDS_LENGTH     =   40;
NSInteger const MAX_STARWORDS_LENGTH4     =   100;

@interface MTNewCardCreateVC ()
@property (weak, nonatomic) IBOutlet UIView *questionView;
@property (weak, nonatomic) IBOutlet UIView *answer1View;
@property (weak, nonatomic) IBOutlet UIView *answer2View;
@property (weak, nonatomic) IBOutlet CustomTextView *questionTV;
@property (weak, nonatomic) IBOutlet UITextField *answer1TV;
@property (weak, nonatomic) IBOutlet UITextField *answer2TV;
@property (weak, nonatomic) IBOutlet UILabel *answer1por;
@property (weak, nonatomic) IBOutlet UILabel *answer2por;
@property (weak, nonatomic) IBOutlet UILabel *questionpor;

//默认第一个
@property (nonatomic,strong) NSString *isLike;
@end

@implementation MTNewCardCreateVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.isLike = @"1";

    [self customNavItem];
    [self setInfo];
    self.view.backgroundColor=  KWhiteColor;

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
- (void)initUI{
//    self.questionView.layer.borderColor = KBlackColor.CGColor;
//    self.questionView.layer.borderWidth = 3;
//    self.answer1View.layer.borderColor = KBlackColor.CGColor;
//    self.answer1View.layer.borderWidth = 3;
//    self.answer2View.layer.borderColor = KBlackColor.CGColor;
//    self.answer2View.layer.borderWidth = 3;
    self.questionView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tv1Choose)];
    [self.questionView addGestureRecognizer:tap1];
    self.answer1View.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tv2Choose)];
    [self.answer1View addGestureRecognizer:tap2];
    self.answer2View.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tv3Choose)];
    [self.answer2View addGestureRecognizer:tap3];
    self.questionTV.textLength = 100;
    self.questionTV.promptLabHiden = NO;

    self.questionTV.promptFont = CLRudeCircleTextFont(12);
    self.questionTV.promptTextColor = TextTiTleColor;
    self.questionTV.textColor = KBlackColor;
    self.questionTV.font = CLRudeCircleTextFont(15);
    self.questionTV.promptFrameMaxX = 20;
    self.questionTV.interception = YES;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewEditChanged3:)
                                                name:@"UITextViewTextDidChangeNotification" object:self.questionTV];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldEditChanged:)
                                                name:@"UITextFieldTextDidChangeNotification" object:self.answer1TV];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textField2EditChanged:)
                                                name:@"UITextFieldTextDidChangeNotification" object:self.answer2TV];
}
- (void)tv1Choose {
    [self.questionTV becomeFirstResponder];
}
- (void)tv2Choose {
    [self.answer1TV becomeFirstResponder];
}
- (void)tv3Choose {
    [self.answer2TV becomeFirstResponder];
}
#pragma mark - 定制导航条内容
- (void)customNavItem {
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectZero text:@"完成"  textcolor:KBlackColor backgroundcolor:nil fontSize:15 haveCorner:NO];
    [rightBtn addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectZero text:@"删除"  textcolor:KBlackColor backgroundcolor:nil fontSize:15 haveCorner:NO];
    [leftBtn addTarget:self action:@selector(leftAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
}
#pragma mark - 按钮事件
- (void)leftAction {
    if (self.deleteInfo) {
        self.deleteInfo(self.dic);
    }
    [self.navigationController popViewControllerAnimated:YES];

}
- (void)rightAction{
    [self.view endEditing:YES];
    NSString *question = self.questionTV.text;
    NSString *answer1 = self.answer1TV.text ;
    NSString *answer2 = self.answer2TV.text;
    if ([[SensitiveWordTools sharedInstance]hasSensitiveWord:question]||[[SensitiveWordTools sharedInstance]hasSensitiveWord:answer1]||[[SensitiveWordTools sharedInstance]hasSensitiveWord:answer2]) {
        MTOkTishiView *okView = [[MTOkTishiView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        okView.content.text = @"涉及敏感词汇！\n请重新编辑 ";
        okView.bgView.backgroundColor = KMainPinkColor;
        okView.goButClick = ^(NSString *  s) {
        };
        [[UIApplication sharedApplication].keyWindow addSubview:okView];
        return;
    }
    if (question.length == 0) {
        [self.view maketishiToast:@"请先输入问题"];
        return;
    }
    if (answer1.length == 0) {
        [self.view maketishiToast:@"请先输入回答1"];
        return;
    }
    if (answer2.length == 0) {
        [self.view maketishiToast:@"请先输入回答2"];
        return;
    }
    NSMutableDictionary *dic = [NSMutableDictionary new];
    dic[@"question"] = question;
    dic[@"answer1"] = answer1;
    dic[@"answer2"] = answer2;
    dic[@"answerfail"] = @"";
    dic[@"isLike"] = self.isLike;
    if (self.insertInfo) {
        self.insertInfo(dic);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)textViewEditChanged3:(NSNotification *)obj {
    UITextView *textField = (UITextView *)obj.object;
    NSString *toBeString = textField.text;
    NSString *lang = [textField.textInputMode primaryLanguage];
    if ([lang isEqualToString:@"zh-Hans"])// 简体中文输入
    {
        //获取高亮部分
        UITextRange *selectedRange = [textField markedTextRange];
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position)
        {
            if (toBeString.length > MAX_STARWORDS_LENGTH4)
            {
                textField.text = [toBeString substringToIndex:MAX_STARWORDS_LENGTH4];
            }
            self.questionpor.text = [NSString stringWithFormat:@"%ld/%ld",textField.text.length,(long)MAX_STARWORDS_LENGTH4];
        }
        
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else
    {
        if (toBeString.length > MAX_STARWORDS_LENGTH4)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:MAX_STARWORDS_LENGTH4];
            if (rangeIndex.length == 1)
            {
                textField.text = [toBeString substringToIndex:MAX_STARWORDS_LENGTH4];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, MAX_STARWORDS_LENGTH4)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
        self.questionpor.text = [NSString stringWithFormat:@"%ld/%ld",textField.text.length,(long)MAX_STARWORDS_LENGTH4];
        
    }
}
#pragma mark - Notification Method
-(void)textFieldEditChanged:(NSNotification *)obj
{
    UITextField *textField = (UITextField *)obj.object;
    NSString *toBeString = textField.text;
    NSString *lang = [textField.textInputMode primaryLanguage];
    if ([lang isEqualToString:@"zh-Hans"])// 简体中文输入
    {
        //获取高亮部分
        UITextRange *selectedRange = [textField markedTextRange];
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position)
        {
            if (toBeString.length > MAX_STARWORDS_LENGTH)
            {
                textField.text = [toBeString substringToIndex:MAX_STARWORDS_LENGTH];
            }
           self.answer1por.text = [NSString stringWithFormat:@"%ld/%ld",textField.text.length,(long)MAX_STARWORDS_LENGTH];
        }
     
     }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else
    {
        if (toBeString.length > MAX_STARWORDS_LENGTH)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:MAX_STARWORDS_LENGTH];
            if (rangeIndex.length == 1)
            {
                textField.text = [toBeString substringToIndex:MAX_STARWORDS_LENGTH];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, MAX_STARWORDS_LENGTH)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
        self.answer1por.text = [NSString stringWithFormat:@"%ld/%ld",textField.text.length,(long)MAX_STARWORDS_LENGTH];

    }
}
-(void)textField2EditChanged:(NSNotification *)obj
{
    UITextField *textField = (UITextField *)obj.object;
    NSString *toBeString = textField.text;
    NSString *lang = [textField.textInputMode primaryLanguage];
    if ([lang isEqualToString:@"zh-Hans"])// 简体中文输入
    {
        //获取高亮部分
        UITextRange *selectedRange = [textField markedTextRange];
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position)
        {
            if (toBeString.length > MAX_STARWORDS_LENGTH)
            {
                textField.text = [toBeString substringToIndex:MAX_STARWORDS_LENGTH];
            }
            self.answer2por.text = [NSString stringWithFormat:@"%ld/%ld",textField.text.length,(long)MAX_STARWORDS_LENGTH];

        }

    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else
    {
        if (toBeString.length > MAX_STARWORDS_LENGTH)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:MAX_STARWORDS_LENGTH];
            if (rangeIndex.length == 1)
            {
                textField.text = [toBeString substringToIndex:MAX_STARWORDS_LENGTH];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, MAX_STARWORDS_LENGTH)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
        self.answer2por.text = [NSString stringWithFormat:@"%ld/%ld",textField.text.length,(long)MAX_STARWORDS_LENGTH];
     }
}

- (void)setInfo {
    if (self.dic) {
        NSDictionary *dic = self.dic;
        NSString *question =  dic[@"question"] ;
        NSString *answer1 =  dic[@"answer1"] ;
        NSString *answer2 =  dic[@"answer2"] ;
        self.questionTV.text = [NSString stringWithFormat:@"%@",question];
        self.answer1TV.text  = [NSString stringWithFormat:@"%@",answer1];
        self.answer2TV.text  = [NSString stringWithFormat:@"%@",answer2];
        self.answer1por.text = [NSString stringWithFormat:@"%ld/%ld",self.answer1TV.text .length,(long)MAX_STARWORDS_LENGTH];
        self.answer2por.text = [NSString stringWithFormat:@"%ld/%ld",self.answer2TV.text .length,(long)MAX_STARWORDS_LENGTH];

    }
}
@end
